const state={
    isCollapse:false,  
    selectMenu:[],
    routerList:[],
    menuActive:localStorage.getItem('menuActive') || '1-1'
}
const mutations = {
    collapseMenu(state){
        state.isCollapse =!state.isCollapse 

    },
    addMenu(state,payload){
        //对数据进行去重
     if(   state.selectMenu.findIndex(item => item.path===payload.path)===-1)
     {
        state.selectMenu.push(payload)
     }
    },
    closeTab(state,payload){
        //找到数据点击的索引
     const index= state.selectMenu.findIndex(val=>val.name===payload.name)
     //通过索引删除指定元素
     state.selectMenu.splice(index, 1)  
    },
    // dynamicMenu(state,payload){ 
  
    //     //通过glob导入文件
    //    const modules= import.meta.glob('../views/**/**/*.vue') 
    //    console.log(modules)
    //    function routerSet(router){
    //     router.forEach(route=>{
    //          //判断没有子菜单，拼接路由数据
    //        if(!route.children){
            
    //         const url = `../views${route.meta.path.startsWith('/') ? '' : '/'}${route.meta.path}/index.vue`
    //        //拿到获取的vue组件
      
    //     route.component = modules[url] ? () => modules[url]() : null;
    //         }else{
    //             routerSet(route.children)
    //         }
    //     })
    //     }
    //    routerSet(payload)
    //    //拿到完整的路由数据
    //    state.routerList=payload 
    // },
    dynamicMenu(state, payload) {
        const modules = import.meta.glob('../views/**/*.vue');
      
        // 递归处理路由配置（包括根路由和嵌套路由）
        const processRoutes = (routes) => {
          routes.forEach(route => {
            // 处理当前路由的组件
            if (!route.children) {
              // 修正路径拼接逻辑
              const cleanPath = route.meta.path.replace(/^\//, '');
              const url = `../views/${cleanPath}/index.vue`;
              route.component = modules[url] || null;
            } else {
              // 递归处理子路由
              processRoutes(route.children);
            }
          });
        };
      
        // 处理完整的路由树
        processRoutes(payload);
        
        // 更新 routerList（包含所有路由）
        state.routerList = payload;
        console.log('Updated routerList:', state.routerList);
      },
    updateMenuActive(state,payload){
    state.menuActive=payload
    localStorage.setItem('menuActive', payload)
    }
}

export default {
    state,
    mutations
}
